package com.camp.android.trip.n.track.trip;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.camp.android.trip.n.track.DatabaseManager;
import com.camp.android.trip.n.track.Item;
import com.camp.android.trip.n.track.NewItemActivity;
import com.camp.android.trip.n.track.R;

public class RecommendationsListAdapter extends BaseAdapter implements OnClickListener {

	private ArrayList<Item> itemList = null;
	private LayoutInflater inflater = null;
	private DatabaseManager dbmanager = null;
	private Activity activity = null;
	
	public RecommendationsListAdapter(LayoutInflater inflater,Activity activity) {
		
		// Add variables
		this.inflater = inflater;
		this.activity = activity;
		this.dbmanager = new DatabaseManager(activity);
		
		// Fill itemList
		itemList = new ArrayList<Item>();
		itemList.add(new Item(1001, "London Eye", Calendar.getInstance(), Calendar.getInstance(), false, 1.5f));
		itemList.add(new Item(1002, "Eat Pudding", Calendar.getInstance(), Calendar.getInstance(), false, 1f));
		itemList.add(new Item(1003, "Eat Pudding again", Calendar.getInstance(), Calendar.getInstance(), false, 1f));
		itemList.add(new Item(1004, "Learn sushi making with crazy brother", Calendar.getInstance(), Calendar.getInstance(), false, 3f));
	}
	
	@Override
	public int getCount() {
		return itemList.size();
	}
 
	@Override
	public Object getItem(int position) {
		Log.i("Tag","Getting item");
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return itemList.get(position).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		// Get custom item view
		View itemView = inflater.inflate(R.layout.recommendations_item, parent, false);
		
		// Get item object
		Item item = (Item)getItem(position);
		
		// Fill custom view with object's properties and disable/hide irrelevant views
		((TextView) itemView.findViewById(R.id.item_name)).setText(item.name);
		((RatingBar) itemView.findViewById(R.id.item_rating)).setRating(item.rating);
		((TextView) itemView.findViewById(R.id.item_id)).setText(item.id+"");
		((TextView) itemView.findViewById(R.id.item_position)).setText(position+"");
		
		// Initialize date format and fill view
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd");
		((TextView) itemView.findViewById(R.id.item_date)).setText(dateFormat.format(item.start.getTime()));
		
		// Add onClickListeners for view
		itemView.setOnClickListener(this);
		return itemView;
	} 

	@Override
	public void onClick(View v) {
		Log.i("TAG","Item Clicked "+ ((TextView)v.findViewById(R.id.item_id)).getText());
		
		int item_position = Integer.parseInt(((TextView)v.findViewById(R.id.item_position)).getText().toString());
		Item item = itemList.get(item_position);
		item.done = false;

		// Create intent to go to TripItineraryActivity
		// And add item's id to extra for initialization
		Intent intent = new Intent(activity,NewItemActivity.class);
		intent.putExtra("trip_id", ""+((TripItineraryActivity)activity).trip_id);
		intent.putExtra("item_name", ((TextView) v.findViewById(R.id.item_name)).getText().toString());
		activity.startActivity(intent);
	}
}
